import logging

from flask import Blueprint, request
from flask import jsonify

from airflow.patsnap.service import pool_service
from airflow.exceptions import AirflowException
from airflow.models import Pool
from airflow.utils.session import create_session

log = logging.getLogger(__name__)

api_speech_pool = Blueprint('api_speech_pool', __name__)


@api_speech_pool.route('/pools/<int:id>', methods=['GET'])
def get_pool(id):
    try:
        with create_session() as session:
            pool = Pool.get_pool_id(id, session)
            return jsonify(code=0, message='ok', data=Pool.to_json(pool))
    except AirflowException as e:
        log.error("get_pool error =%s, params=%s", e, id, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_pool.route('/pools', methods=['GET'])
def get_pools():
    try:
        page = request.args.get('page', 0, type=int)
        result = pool_service.get_pools(page)
        return jsonify(code=0, message='ok', data=result)
    except AirflowException as e:
        log.error("get_pools error =%s", e, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_pool.route('/pools', methods=['POST'])
def add_pool():
    try:
        name = request.values.get("pool", type=str, default=None)
        slots = request.values.get("slots", type=int, default=None)
        desc = request.values.get("desc", type=str, default=None)
        if name is None:
            log.error("add_pool error pool=%s", name, )
            return jsonify(code=400, message="add_pool error name=None", data=None)
        if slots is None:
            log.error("add_pool error slots=%s", slots, )
            return jsonify(code=400, message="add_pool error slots=None", data=None)
        if desc is None:
            log.error("add_pool error desc=%s", slots, )
            return jsonify(code=400, message="add_pool error desc=None", data=None)

        pool_service.add_pool(name, slots, desc)
        return jsonify(code=0, message='ok', data=None)
    except AirflowException as e:
        log.error("add_pool error =%s,", e, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_pool.route('/pools/<int:id>', methods=['DELETE'])
def delete_pool(id):
    try:
        pool_service.delete_pool(id)
        return jsonify(code=0, message='ok', data=None)
    except AirflowException as e:
        log.error("delete_pool error =%s, params=%s", e, id, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_pool.route('/pools', methods=['PUT'])
def update_pool():
    try:
        id = request.values.get("id", type=int, default=None)
        name = request.values.get("pool", type=str, default=None)
        slots = request.values.get("slots", type=int, default=None)
        desc = request.values.get("desc", type=str, default=None)
        if id is None:
            log.error("update_pool error id=%s", id, )
            return jsonify(code=400, message="add_pool error id=None", data=None)
        if name is None:
            log.error("add_pool error pool=%s", name, )
            return jsonify(code=400, message="add_pool error name=None", data=None)
        if slots is None:
            log.error("add_pool error slots=%s", slots, )
            return jsonify(code=400, message="add_pool error slots=None", data=None)
        if desc is None:
            log.error("add_pool error desc=%s", slots, )
            return jsonify(code=400, message="add_pool error desc=None", data=None)

        pool_service.update_pool(id, name, slots, desc)
        return jsonify(code=0, message='ok', data=None)
    except AirflowException as e:
        log.error("update_pool error =%s,", e, )
        return jsonify(code=500, message=str(e), data=None)
